package com.shujia

import java.io.{BufferedInputStream, FileInputStream}
import scala.io.{BufferedSource, Source}

object Code03ReadFile {
  def main(args: Array[String]): Unit = {

    Source
      .fromFile("scala_code/data/words.txt")
      .getLines()  // 按行读取数据
      .foreach(println)

    val fileStr: String = Source
      .fromFile("scala_code/data/words.txt")
      .getLines()   // 获取数据时,并不包含其换行符
      .mkString
    println(fileStr)

    // 通过Java读取文件数据，再将输入流放入值Source中
    // fromInputStream(is: InputStream, enc: String):  第一个参数是Java中的InputStream类 第二个参数可以使用UTF-8设置字符集
    val stream = new FileInputStream("scala_code/data/words.txt")
    Source
      .fromInputStream(stream,"UTF-8")
      .getLines()
      .foreach(println)

    stream.close()


    val source: BufferedSource = Source
      .fromFile("scala_code/data/words.txt")

    val fileData: Iterator[String] = source.getLines()
    while(fileData.hasNext){
      val lineStr: String = fileData.next()
      println(lineStr)
    }




  }
}
